package com.sgb.trip.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.link.entity.trip.AirportCityEntity;
import com.sgb.link.entity.trip.BaseCityEntity;
import com.sgb.trip.api.TripNet;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CitySelectionViewModel extends BaseViewModel {
    public MutableLiveData<List<AirportCityEntity>> ldData = new MutableLiveData<>();
    public MutableLiveData<List<AirportCityEntity>> ldDataHot = new MutableLiveData<>();
    public MutableLiveData<List<AirportCityEntity>> ldDataQuery = new MutableLiveData<>();

    public CitySelectionViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /****
     * 获取城市
     */
    public void getCitySelectionList() {
        TripNet.getInstance().getService().getCitySelectionList().enqueue(new ICallBack<BaseEntity<BaseCityEntity<List<AirportCityEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<BaseCityEntity<List<AirportCityEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null) {
                    return;
                }

                if (data.getData() != null) {
                    ldData.postValue(data.getData().getAllCity());
                    ldDataHot.postValue(data.getData().getHotCity());
                }
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /****
     * 模糊搜索
     */
    public void queryCity(String cityName) {
        TripNet.getInstance().getService().queryCity(cityName).enqueue(new ICallBack<BaseEntity<List<AirportCityEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AirportCityEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null) {
                    return;
                }

                if (data.getData() != null) {
                    ldDataQuery.postValue(data.getData());
                }
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
